package com.SouthernWall_404.age_tool.common.world.item;

import com.SouthernWall_404.age_tool.common.data.tags.BlockTags;
import com.SouthernWall_404.age_tool.common.tag.ModTags;
import com.mojang.brigadier.Command;
import net.minecraft.ChatFormatting;
import net.minecraft.core.BlockPos;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.*;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.Interaction;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.flag.FeatureFlagSet;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.ClipContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.ChestBlock;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.ChestBlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.chunk.ChunkAccess;
import net.minecraft.world.level.chunk.LevelChunk;
import net.minecraft.world.level.levelgen.structure.Structure;
import net.minecraft.world.level.levelgen.structure.StructureStart;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.HitResult;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.common.ForgeTier;
import net.minecraftforge.common.Tags;
import net.minecraftforge.registries.DeferredRegister;

import java.util.Map;

public class ChestLootCheckerItem extends Item{

    @Override
    public boolean isEnabled(FeatureFlagSet p_249172_) {
        return super.isEnabled(p_249172_);
    }

    public ChestLootCheckerItem(Properties p_41383_) {
        super(p_41383_);
    }

    @Override
    public InteractionResult useOn(UseOnContext context) {

        Player player = context.getPlayer();
        Level level=context.getLevel();
        BlockPos blockPos=context.getClickedPos();
        BlockState blockState=level.getBlockState(blockPos);
        ChunkAccess chunk=level.getChunkAt(blockPos);

        if(level.isClientSide)
        {
            return InteractionResult.SUCCESS;
        }

//        if(blockState.is(ModTags.CONTAINERS) )
//        {
            BlockEntity blockEntity=level.getBlockEntity(blockPos);
            if(blockEntity!=null)
            {
                CompoundTag nbt =blockEntity.saveWithFullMetadata();


                if (nbt.contains("LootTable")) {

                    String lootTableStr = nbt.getString("LootTable");
                    ResourceLocation lootTable = new ResourceLocation(lootTableStr);
                    MutableComponent mutableComponent =ComponentUtils.wrapInSquareBrackets(Component.translatable(
                            "chat.age_tool.chest_loot",
                            lootTable)
                            .withStyle(style -> style
                                    .withColor(ChatFormatting.GREEN)
                                    .withClickEvent(
                                            new ClickEvent(
                                                    ClickEvent.Action.COPY_TO_CLIPBOARD,
                                                    lootTable.toString()))
                                    .withHoverEvent(
                                            new HoverEvent(HoverEvent.Action.SHOW_TEXT,
                                                    Component.translatable("chat.age_tool.chest.tooltip")
                                                            .withStyle(style1 -> style1
                                                            .withColor(ChatFormatting.GREEN))

                                            ))));

                    player.sendSystemMessage(Component.translatable("message.age_tool.loot.check",blockState.getBlock().getName(),mutableComponent));
                } else {
                    player.sendSystemMessage(Component.translatable("message.age_tool.loot.check.failed",blockState.getBlock().getName()));
                }

            }
//        }

//        }
//        if(chunk.hasAnyStructureReferences())
//        {
//            Map<Structure, StructureStart> structureStarts = chunk.getAllStarts();
//
//            for (Map.Entry<Structure, StructureStart> entry : structureStarts.entrySet()) {
//                Structure currentStructure = entry.getKey();
//                StructureStart start = entry.getValue();
//                if (start.getBoundingBox().isInside(blockPos)) {
//                    // 坐标属于当前结构
//
//                }
//        }

        return InteractionResult.SUCCESS;
    }
}
